<?php

namespace app\web\services;

use think\App;
use think\facade\Request;
use think\Service;
use app\web\model\NavModel;
use app\web\model\ProblemModel;
use app\web\model\GuestbookModel;

class ProblemService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        $this->NavModel = new NavModel();
        $this->ProblemModel = new ProblemModel();
        $this->GuestbookModel = new GuestbookModel();
        parent::__construct($request, $app);
    }

    /**
     * 获取标题导航
     */
    public function getNavMsg()
    {
        $res = $this->NavModel->getNav();
        if (!$res) {
            return $this->_getResponse("NUKNOW_EORRE");
        }
        $pathinfo = '/' . Request::pathinfo();
        foreach ($res as $k => $v) {
            if ($v['url'] == $pathinfo) {
                $res[$k]['states'] = 1;
            } else {
                $res[$k]['states'] = 0;
            }
        }
        return $this->_getResponse("SUCCESS", $res);
    }


    /**
     * @param 获取问题列表
     */
    public function listt(){
        $post = Request::param();
        $where = [['a.enable','eq',1],['a.show','eq',1]];
        if(!empty($post['title'])){
            $where = [['title','like','%'.$post['title'].'%']];
            $query =['title'=>$post['title']];
        }
        $list = $this->ProblemModel->listt($where,$query=[]);
        if(!$list){
            return $this->_getResponse("NUKNOW_EORRE");
        }
        return $list;
    }


    /**
     * @param 获取问题详情
     */
    public function potlist(){
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['a.id','eq',$post['id']]];
        $rulset = $this->ProblemModel->pomolist($where);
        if(!$rulset){
            return $this->_getResponse("DATA_ERROR");
        }
        return $rulset;
    }


    /**
     * @param 获取当前留言信息
     */
    public function pocket(){
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['probleid','eq',$post['id']]];
        $rulset = $this->GuestbookModel->conectlist($where);
        if(empty($rulset)){
           return  $rulset = [];
        }
        $array = [];
        foreach ($rulset as $value){
            $value['position'] = opstion($value['position']);
            $array[] = $value;
        }
        return $array;
    }

    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "DATA_ERROR" => ["status" => 7, "msg" => "数据不存在", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "EDIT_SUCCESS" => ["status" => 0, "msg" => "修改成功", "data" => $data],
            "CONFIRM_SUCCESS" => ["status" => 0, "msg" => "确认成功", "data" => $data],
            "EMLIE_SUCCESS" => ["status" => 0, "msg" => "留言成功", "data" => $data]
        ];
        return $maps[$key];
    }

}